package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.exception.BaseException;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.PageResult;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 新增地址
     * @param addressBook 地址簿实体类
     */
    public void save(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.intert(addressBook);
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @return AddressBook集合
     */
    public List<AddressBook> showAddressBook() {
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> addressBookList = addressBookMapper.list(addressBook);
        return addressBookList;
    }
    /**
     * 查询默认地址
     * @return AddressBook实体类
     */
    public AddressBook getDefault() {
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        List<AddressBook> addressBookList = addressBookMapper.list(addressBook);
        // 如果查询结果不为空并且仅仅查询到一个地址，则返回该地址
        if (addressBookList != null && addressBookList.size() == 1){
            addressBook = addressBookList.get(0);
        } else {
            // 如果查询结果为空或者查询到多个地址，则抛出异常
            throw new BaseException(MessageConstant.AddressBook_NOT_FOUND);
        }
        return addressBook;
    }

    /**
     * 根据id查询地址
     * @param id 地址id
     * @return AddressBook实体类
     */
    public AddressBook getById(Long id) {
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        List<AddressBook> addressBookList = addressBookMapper.list(addressBook);
        if (addressBookList != null && addressBookList.size() == 1){
            addressBook = addressBookList.get(0);
        }
        return addressBook;
    }

    /**
     * 根据id修改地址
     * @param addressBook 地址簿实体类
     */
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id删除地址
     * @param id 地址id
     */
    public void delete(Long id) {
        addressBookMapper.deleteByid(id);
    }

    /**
     * 设置默认地址
     * @param addressBook 地址簿实体类
     */
    public void setDefault(AddressBook addressBook) {
        // 将所有的地址设为非默认地址
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateByDefault(addressBook);
        // 修改当前地址为默认地址
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }
}
